package jface.solutions;

import jface.data.Person;
import jface.data.Person.Gender;
import jface.solutions.util.AgeEditingSupportExercise;
import jface.solutions.util.GenderEditingSupportExercise;
import jface.solutions.util.PersonFirstEditingSupportExercise;
import jface.util.PersonLabelProvider;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;

public class JFaceTableEditExercise {
	
	static Person[] persons = new Person[10];
	
	
	public static void main(String[] args) {
		
		for (int i = 0; i < persons.length; i++) {
			persons[i] = new Person("First "+i, "Last "+i, i*7, i > 5 ? Gender.female : Gender.male);
		}
		
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
	
		TableViewer tableViewer = new TableViewer(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.getTable().setLinesVisible(true);

		TableViewerColumn tvcFirst = new TableViewerColumn(tableViewer,SWT.NONE);
		tvcFirst.setEditingSupport(new PersonFirstEditingSupportExercise(tableViewer));
		TableColumn tcFirst = tvcFirst.getColumn();
		tcFirst.setText("First Name");
		tcFirst.setResizable(true);
		tcFirst.setMoveable(true);
		tcFirst.pack();

		TableViewerColumn tvcLast = new TableViewerColumn(tableViewer,SWT.NONE);
		TableColumn tcLast = tvcLast.getColumn();
		tcLast.setText("Last Name");
		tcLast.setResizable(true);
		tcLast.setMoveable(true);
		tcLast.pack();

		TableViewerColumn tvcAge = new TableViewerColumn(tableViewer,SWT.NONE);
		tvcAge.setEditingSupport(new AgeEditingSupportExercise(tableViewer));
		TableColumn tcAge = tvcAge.getColumn();
		tcAge.setText("Age");
		tcAge.setResizable(true);
		tcAge.setMoveable(true);
		tcAge.pack();

		TableViewerColumn tvcGender = new TableViewerColumn(tableViewer,SWT.NONE);
		tvcGender.setEditingSupport(new GenderEditingSupportExercise(tableViewer));
		TableColumn tcGender = tvcGender.getColumn();
		tcGender.setText("Gender");
		tcGender.setResizable(true);
		tcGender.setMoveable(true);
		tcGender.pack();
		
		tableViewer.setLabelProvider(new PersonLabelProvider());
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(persons);
		
		tableViewer.getTable().pack();
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();
	}

}
